home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TEXTEDIT.SWG / 0010_Text Word Wrap.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-03  |  3KB  |  80 lines

  1.  
  2. {
  3.    Here's some code I found in this echo a number of years ago - I don't
  4. recall who should get credit for it.  I put it into my own program,
  5. which uses some other Units, and I hope I've sanitized it enough to make
  6. it generic...
  7.  
  8. Uses a FASTWRITE routine that can be found in SWAG G.D. 02/01/94 }
  9.  
  10.  
  11. program WordWrap;
  12. uses CRT;
  13. const
  14.    FKeyCode          = #00;
  15.    Space             = ' ';
  16.    Hyphen            = '-';
  17.    BackSpace         = #08;
  18.    C_R               = #13;
  19.    MaxWordLineLength = 60;
  20.    MAXLINES          = 6;  { Maximum # of lines in "box" }
  21. var
  22.    WordLine  : string[MaxWordLineLength];
  23.    Index1    : byte;
  24.    Index2    : byte;
  25.    InputChar : char;
  26.    LINE      : byte;               { current output line }
  27.    LC        : byte;                        { Line Count }
  28.    I         : Word;
  29.    S1        : string;
  30.    LA        : array[1..MAXLINES] of string[MaxWordLineLength];
  31. begin
  32.   WordLine := ''; Index1 := 0; Index2 := 0; InputChar := Space;
  33.   ClrScr; Write ('Enter text (',MAXLINES:0,' line maximum): ');
  34.   for I := 1 to MAXLINES do  { clear storage array }
  35.     LA[I] := '';
  36.   InputChar := ReadKey;
  37.   LC := 1; LINE := 6; gotoXY (1,20);               { work area }
  38.   while LC <= MAXLINES do
  39.     begin
  40.       case InputChar of
  41.         #13      : begin                { C/R - terminate line }
  42.                      S1 := WordLine;
  43.                      Writeln (S1); LA[LC] := S1; Inc(LC);
  44.                      gotoXY (1,20); ClrEol; WordLine := ''
  45.                    end;
  46.         BackSpace:
  47.           begin
  48.             Write(BackSpace,Space,BackSpace);
  49.             if Length(WordLine) > 0 then Dec(WordLine[0])
  50.           end;
  51.         FKeyCode:                         { flush function key }
  52.           begin
  53.             InputChar := ReadKey; InputChar := Space
  54.           end
  55.         else                                      { valid char }
  56.           begin
  57.             Write(InputChar); WordLine := WordLine+InputChar;
  58.             if (Length(WordLine) >= (MaxWordLineLength - 1)) then
  59.               begin                  { have to do a word-wrap }
  60.                 Index1 := MaxWordLineLength-1;
  61.                 while ((WordLine[Index1] <> Space) and
  62.                        (WordLine[Index1] <> Hyphen) and
  63.                        (Index1 <> 0))
  64.                   do Dec(Index1);
  65.                 if (Index1 = 0) then  {no space was found to split!}
  66.                   Index1 := (MaxWordLineLength-1);    {forces split}
  67.                 S1 := Copy(WordLine,1,Index1);
  68.                 Delete(WordLine,1,Index1);
  69.                 for Index2 := 1 TO LENGTH(WordLine) do
  70.                   Write(BackSpace,Space,BackSpace);
  71.                 FastWrite (1,LINE,LONORM,S1); Inc(LINE);
  72.                 LA[LC] := S1; Inc(LC);
  73.                 gotoXY (1,20) ClrEol; Write(WordLine)
  74.               end
  75.           end
  76.       end;                                          {case InputChar}
  77.       InputChar := ReadKey                  {Get next key from user}
  78.     end;                       {while (InputChar <> CarriageReturn)}
  79. end.
  80.